package net.zjitc.config;


import net.zjitc.interceptor.AdminInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@EnableWebMvc
@Configuration
@ComponentScan("net.zjitc.controller")
public class SpringMvcConfig implements WebMvcConfigurer {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable("default");
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp().prefix("/").suffix(".jsp");

    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AdminInterceptor()).addPathPatterns("/admin/*").addPathPatterns("/category/*").addPathPatterns("/goods/*").addPathPatterns("setting/*").excludePathPatterns("/phone/**").excludePathPatterns("/admin/login");
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8081","http://localhost:8083")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //前面的upload意思是，访问的时候路径上要加上upload，不然也访问不到。
        //后面的upload意思是，你图片上传的路径，我的图片上传就在upload文件中(新创建的的文件夹)。
        registry.addResourceHandler("/static/**").addResourceLocations("file:static/");
    }

}